/**
 * 
 */
package com.huawei.esdk.uc.sametime.toolbar.actions;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.widgets.DialPad;
import com.ibm.collaboration.realtime.im.community.CommunityLoginEvent;
import com.ibm.collaboration.realtime.imhub.actions.BuddyListAction;

/**
 * @author y00175154
 * 
 */
public class EsdkDialCallAction extends BuddyListAction
{

    private DialPad dialpad;

    /**
	 * 
	 */
    public EsdkDialCallAction()
    {
        super(null);
        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getShell();
        dialpad = new DialPad(shell);
    }

    @Override
    protected void handleCommunityLoginEvent(CommunityLoginEvent arg0)
    {
        super.handleCommunityLoginEvent(arg0);
        // 在检测到sametime登出时,关闭拨号盘
        if (CommunityLoginEvent.TYPE_LOGGED_OUT == arg0.getType())
        {
            Display.getDefault().syncExec(new Runnable()
            {

                @Override
                public void run()
                {
                    dialpad.dispose();
                }
            });
        }
    }

    public boolean isEnabled()
    {
        // 这里判断插件是否登录UC
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        // 这里判断插件是否登录sametime
        boolean isSametimeLogin = GlobalConfiguration.getInstance()
                .isSamtimeLogin();
        // boolean isInCall = (CallStatus.end != UCCallService.getCallStatus());

        // 只有同时登录了uc和sametime,才能使用工具栏上的功能
        if (isSametimeLogin && isUCLogin/* && !isInCall */)
        {
            return true;
        }

        return false;
    }

    public void run()
    {
        boolean isInCall = (CallStatus.end != UCCallService.getCallStatus());
        if (!isInCall)
        {
            Rectangle rect = getToolItemBounds();
            Shell dialPadShell = dialpad.getShell();
            if (rect != null)
            {
                dialPadShell.setLocation(rect.x, rect.y + rect.height);
            }
            dialPadShell.open();
        }
        else
        {
            MessageDialog
            .openInformation(
                    Display.getCurrent().getActiveShell(),
                    Messages.TIPS,
                    Messages.IS_CALLING);
        }
    }

}
